#include "print_info.h"

print_info::print_info()
{

}

// 辅助函数：将角色枚举转换为字符串
QString print_info::roleToString(pjsip_role_e role) {
    switch(role) {
        case PJSIP_ROLE_UAC: return "Caller (UAC)";
        case PJSIP_ROLE_UAS: return "Callee (UAS)";
        default: return QString("Unknown Role (%1)").arg(static_cast<int>(role));
    }
}

// 辅助函数：将呼叫状态枚举转换为字符串
QString print_info::invStateToString(pjsip_inv_state state) {
    switch(state) {
        case PJSIP_INV_STATE_NULL:        return "Null";
        case PJSIP_INV_STATE_CALLING:      return "Calling";
        case PJSIP_INV_STATE_INCOMING:    return "Incoming";
        case PJSIP_INV_STATE_EARLY:       return "Early";
        case PJSIP_INV_STATE_CONNECTING:   return "Connecting";
        case PJSIP_INV_STATE_CONFIRMED:   return "Confirmed";
        case PJSIP_INV_STATE_DISCONNECTED:return "Disconnected";
        default: return QString("Unknown State (%1)").arg(static_cast<int>(state));
    }
}

// 辅助函数：格式化时间值为可读字符串
QString print_info::formatTimeVal(const TimeVal &tv) {
    // 假设TimeVal包含秒和微秒（类似struct timeval）
    long totalSeconds = tv.sec;
    int hours = totalSeconds / 3600;
    int minutes = (totalSeconds % 3600) / 60;
    int seconds = totalSeconds % 60;
    int milliseconds = tv.msec / 1000;

    return QString("%1:%2:%3.%4")
        .arg(hours, 2, 10, QLatin1Char('0'))
        .arg(minutes, 2, 10, QLatin1Char('0'))
        .arg(seconds, 2, 10, QLatin1Char('0'))
        .arg(milliseconds, 3, 10, QLatin1Char('0'));
}

// 辅助函数：打印单个媒体信息
void print_info::printMediaInfo(const CallMediaInfo &media) {
    qDebug() << "  Media Type:"  ;/* 添加媒体类型转换逻辑 */
    qDebug() << "  Direction:" ; /* 添加方向转换逻辑 */
    // 根据实际结构添加更多信息
}

// 主打印函数
void print_info::printCallInfo(const CallInfo &info) {
    qDebug().noquote() << "\n======= Call Information =======";
    qDebug() << "ID:" << info.id;
    qDebug() << "Role:" << roleToString(info.role);
    qDebug() << "Account ID:" << info.accId;
    qDebug() << "Local URI:" << QString::fromStdString(info.localUri);
    qDebug() << "Local Contact:" << QString::fromStdString(info.localContact);
    qDebug() << "Remote URI:" << QString::fromStdString(info.remoteUri);
    qDebug() << "Remote Contact:" << QString::fromStdString(info.remoteContact);
    qDebug() << "Call ID String:" << QString::fromStdString(info.callIdString);
    // 呼叫设置（需要根据实际结构扩展）
    qDebug() << "\nCall Settings:";
    qDebug() << "  Audio/Video Count:" << info.setting.audioCount << "/" << info.setting.videoCount;
    // 状态信息
    qDebug() << "\nState:" << invStateToString(info.state)
             << "(" << QString::fromStdString(info.stateText) << ")";
    qDebug() << "Last Status:" << info.lastStatusCode
             << "-" << QString::fromStdString(info.lastReason);
    // 活动媒体
    qDebug() << "\nActive Media (" << info.media.size() << "):";
    for (const auto &media : info.media) {
        printMediaInfo(media);
    }
    // 临时媒体
    qDebug() << "\nProvisional Media (" << info.provMedia.size() << "):";
    for (const auto &media : info.provMedia) {
        printMediaInfo(media);
    }
    // 持续时间
    qDebug() << "\nDurations:";
    qDebug() << "Connected:" << formatTimeVal(info.connectDuration);
    qDebug() << "Total:" << formatTimeVal(info.totalDuration);
    // 远程信息
    qDebug() << "\nRemote Info:";
    qDebug() << "Offerer:" << (info.remOfferer ? "Yes" : "No");
    qDebug() << "Offered Audio/Video:" << info.remAudioCount << "/" << info.remVideoCount;
    qDebug() << "=================================\n";
}

void print_info::printAccountInfo(const AccountInfo &account) {
    qDebug()<<endl;
    qDebug()<<"Account info ************************************************************";
    // 打印帐户ID
    qDebug() << QString("  ID: %1").arg(account.id);
    // 打印是否是默认帐户
    qDebug() << QString("  Is Default: %1").arg(account.isDefault ? "Yes" : "No");
    // 打印帐户URI
    qDebug() << QString("  URI: %1").arg(account.uri.c_str());
    // 打印注册配置状态
    qDebug() << QString("  Registration Configured: %1").arg(account.regIsConfigured ? "Yes" : "No");
    // 打印当前注册状态
    qDebug() << QString("  Registration Active: %1").arg(account.regIsActive ? "Yes" : "No");
    // 打印过期时间（以秒为单位）
    qDebug() << QString("  Expires in: %1 sec").arg(account.regExpiresSec);
    // 打印注册状态码
    qDebug() << QString("  Registration Status Code: %1 ()")
                .arg(account.regStatus)
                .arg(account.regStatus == PJSIP_SC_OK ? "OK" : "Unknown");
    // 打印注册状态文本
    qDebug() << QString("  Registration Status Text: %1").arg(account.regStatusText.c_str());
    // 打印最后的注册错误
    qDebug() << QString("  Last Registration Error: %1").arg(account.regLastErr);
    // 打印在线状态
    qDebug() << QString("  Online Status: %1").arg(account.onlineStatus ? "Online" : "Offline");
    // 打印在线状态文本
    qDebug() << QString("  Online Status Text: %1").arg(account.onlineStatusText.c_str());
    qDebug()<<"end info ************************************************************";
    qDebug()<<endl;
}

void print_info::printTransInfo(const TransportInfo &info)
{
    qDebug()<<endl;
    qDebug()<<"Account info ************************************************************";
    qDebug()<<"TransInfo.id :"<<info.id;
    qDebug()<<"TransInfo.info :"<<QString::fromStdString(info.info);
    qDebug()<<"TransInfo.type :"<<info.type;
    qDebug()<<"TransInfo.flags :"<<info.flags;
    qDebug()<<"TransInfo.typeName :"<<QString::fromStdString(info.typeName);
    qDebug()<<"TransInfo.localName :"<<QString::fromStdString(info.localName);
    qDebug()<<"TransInfo.usageCount :"<<info.usageCount;
    qDebug()<<"TransInfo.localAddress :"<<QString::fromStdString(info.localAddress);
    qDebug()<<"end info ************************************************************";
    qDebug()<<endl;
}
